<?php
/**
 * Created by PhpStorm.
 * User: wo
 * Date: 2019/8/13
 * Time: 14:54
 */

namespace Home\Controller;


class OrderLogisticsController
{
    /**
     * 根据订单号查询物流信息
    */
    public function logisticsInformation(){
        $order_sn = I("post.order_sn");
        if(!$order_sn)render(10004,'order_sn参数错误');
        //实例化数据库
        $order = M("order");
        $orderGoods = M("orderGoods");
        $orderInfo = $order->where(array('order_sn'=>$order_sn))->find();
        try{
            $result = array();
            if(!$orderInfo)render(20002,'订单信息有误',$result);
            $type = $orderGoods->alias('g')
                ->join("left join ecm_goods_spec c on c.spec_id = g.spec_id")
                ->join("left join ecm_storestock s on c.spec_3value = s.storestock_id")
                ->where(array('g.order_id'=>$orderInfo['order_id']))
                ->field("s.stock_style")
                ->find();

            // todo 确定订单是已发货状态 以及查看订单类型
            if($type['stock_style'] && in_array($orderInfo['status'],array(30,40))){
                 //发货方式(1保税仓,2国外直邮,3国内现货发货)
                if($type['stock_style'] != 3){
                    $result = $this->expressOrder($orderInfo);
                }else{
                    $result = $this->index($orderInfo);
                }
                // todo 暂无物流信息
                if(empty($result)){
                    render(10000,'暂无物流信息',$result);
                }
                $result['express_name'] = $orderInfo['express_name']?$orderInfo['express_name']:'';
                render(10000,'success',$result);
            }else{
                render(20003,'订单类型不符合当前操作',$result);
            }
        }catch(\Exception $e){
            render(20001,'网络错误',array());
        }
    }


    //查询国内快递一百轨迹信息
    private function index($orderInfo){
        try{
            //接入逻辑处理
            $newArr = array('com'=>$orderInfo['express_code'],'num'=>$orderInfo['express_no']);
            $json = json_encode($newArr);

            $post_data = array();
            //获取配置项
            $post_data["customer"] = C('Kuaidi_customer');
            $key= C('Kuaidi_key');
            $post_data["param"] = $json;

            //请求url
            $url = C('Kuaidi_url');
            $post_data["sign"] = md5($post_data["param"].$key.$post_data["customer"]);
            $post_data["sign"] = strtoupper($post_data["sign"]);
            $o="";
            foreach ($post_data as $k=>$v)
            {
                $o.= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
            }
            $post_data=substr($o,0,-1);
            $result = json_decode(request_post($url, $post_data),true);

            //逻辑处理
            $arr = array();
            if($result['status'] == 200 && $result['message'] == 'ok'){
                //重新组织参数
                foreach($result['data'] as $k=>$v){
                    $arr['express_code'] = $result['com']; //物流公司编码
                    $arr['track_no'] = $result['nu'];      //运单号
                    $arr['state'] = express_status($result['state']);       //物流当前状态
                    $arr['data'][$k] = array(
                            'content_text' => $v['context'],       //物流轨迹信息
                            'time'         => $v['time']
                    );
                }
                return $arr;
            }else{
                return $arr;
            }
        }catch(\Exception $e){
            render(20001,'网络错误');
        }
    }

    //易客满物流轨迹查询
    private function expressOrder($orderInfo){
        $post_data = array(
            'trackNo' => $orderInfo['order_sn']
        );
        $url = C('ERP_URL').'/api/express/ExpressOrder_Select'; #url 地址必须 http://xxxxx
        $res = sync_data($url,$post_data);
        $arr = array();
        if($res['status'] == 200){
            //todo 声明一个新数组  进行排列组合
            $data = $res['data'][0]['trackingEventDetails'];
            //todo 根据二维数组中的摸一个字段进行排序
            $last_names = array_column($data,'eventDateTime');
            array_multisort($last_names,SORT_DESC,$data);

            foreach($data as $k=>$v){
                $arr['express_code'] = $v['eventLocationCode'];        //物流公司编码
                $arr['track_no']     = $res['data'][0]['ecmstrackNo'];        //运单号
                $arr['state'] = '已发货';
                if($orderInfo['status'] == 40){
                    $arr['state'] = '已签收';
                }
                $arr['data'][$k]    = array(
                    'content_text' => $v['eventCodeDesc'],       //物流轨迹信息
                    'time'         => date('Y-m-d H:i:s',strtotime($v['eventDateTime']))
                );
            }
            return $arr;
        }else{
            return $arr;
        }
    }


}